package com.zhiwang.planet.widget

import android.content.Context
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.zhiwang.planet.R

class IconTextView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

    var Icon: AppCompatImageView
    var Title: AppCompatTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.icon_text_view_layout, null)
        Icon = view.findViewById(R.id.icon)
        Title = view.findViewById(R.id.title)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.IconTextView)
        val resourceId = typedArray.getResourceId(R.styleable.IconTextView_icon, 0)
        if (resourceId != 0) Icon.setImageResource(resourceId)
        Title.text = typedArray.getString(R.styleable.IconTextView_text)

        val margin = typedArray.getDimensionPixelSize(R.styleable.IconTextView_margin, 0)
        val lp = Title.layoutParams as LinearLayout.LayoutParams
        lp.topMargin = margin
        Title.layoutParams = lp
        typedArray.recycle()
        addView(view)
    }
}